package com.esiag.pdsteam.measureagainst;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.esiag.isidis.fi.pdsTeam.jms.chooser.IActionChooser;
import com.esiag.isidis.fi.pdsTeam.jms.chooser.impl.ActionChooser;
import com.esiag.isidis.fi.pdsTeam.jms.producer.ActiveMQProducer;
import com.esiag.isidis.fi.pdsTeam.producer.IProducer;
import com.esiag.isidis.fi.pdsTeam.receive.beans.Action;
import com.esiag.pdsteam.dao.ActionDao;
import com.esiag.pdsteam.entities.Information;
import com.esiag.pdsteam.messagemanager.ActionDeliver;
import com.esiag.pdsteam.messagemanager.ActionExpander;
import com.esiag.pdsteam.service.InformationService;
import com.esiag.pdsteam.treatcomplexinfo.FindComplexInfo;

public class ManageMeasureAgainst {

	ApplicationContext context = 
			new ClassPathXmlApplicationContext("classpath:test-context/FindComplexInfo-context.xml");

	@Autowired
	FindComplexInfo findComplexInfo=new FindComplexInfo();
	@Autowired
	InformationService informationService=context.getBean(InformationService.class);
	@Autowired
	ActionDao actionDao=context.getBean(ActionDao.class);
	IProducer producer;
	public ManageMeasureAgainst() {

	}

	public int treatInformationMeasureAgainst(Information information)
	{
		FileHandler fileHandler;
		
	
			try {
				fileHandler = new FileHandler("GhitaBenMeasureAgainst.log");
		
			final Logger logger = Logger.getLogger("loger");
			logger.addHandler( fileHandler);
			SimpleFormatter formatter = new SimpleFormatter();  
            fileHandler.setFormatter(formatter);  
            
            
            
		findComplexInfo.manageComplexInfo(information);
		System.out.println(findComplexInfo.get_piles());
		logger.log(Level.INFO, "Complex and Simple info : " + findComplexInfo.get_piles());

		for(Integer mapKey : findComplexInfo.get_piles().keySet())
		{
			
			if(mapKey!=0)
			{
				//System.out.println("voici la contre mesure automatique :"+ informationService.getById(mapKey).getAction().getDescription());
				if(informationService.getById(mapKey).getAction().getId()!=0)
				{	
					logger.log(Level.INFO, "voici la contre mesure automatique :" + informationService.getById(mapKey).getAction().getDescription());
					System.out.println("voici la contre mesure automatique :"+ informationService.getById(mapKey).getAction().getDescription());
				IActionChooser iActionChooser = new ActionChooser();
				com.esiag.isidis.fi.pdsTeam.receive.beans.Action action= new com.esiag.isidis.fi.pdsTeam.receive.beans.Action();
				action.setId(informationService.getById(mapKey).getAction().getId());
				ArrayList<Action> actionsList = new ArrayList<Action>();
				actionsList.add(action);
				action.setActionsList(actionsList);



				ActionChooser chooser = new ActionChooser();
				ActionExpander acEx = new ActionExpander();
				chooser.setmExpenderExecuter(acEx);
				ActionDeliver acDe = new ActionDeliver();
				chooser.setmNetFieldExecuter(acDe);

				chooser.chooseAction(action);
				return 1;
				}
				else
				{
					logger.log(Level.INFO, "l information envoyé au centre de controle est :" + informationService.getById(mapKey).getDescription());
					System.out.println("l information envoyé au centre de controle est :"+ informationService.getById(mapKey).getDescription());
					producer=new ActiveMQProducer(com.esiag.isidis.fi.pdsTeam.send.beans.Information.class);
					com.esiag.isidis.fi.pdsTeam.send.beans.Information information2 = new com.esiag.isidis.fi.pdsTeam.send.beans.Information();
					information2.setId(informationService.getById(mapKey).getId());
					information2.setDescri(informationService.getById(mapKey).getDescription());
					try {
						producer.send(information2);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return 2;
				}
			}


			else{

				for(Integer Key : findComplexInfo.get_piles().get(0).keySet())
				{

					if(informationService.getById(Key).getAction().getId()!=0)

					{
						logger.log(Level.INFO, "voici la contre mesure automatique :" + informationService.getById(Key).getAction().getDescription());
						System.out.println("voici la contre mesure automatique :"+ informationService.getById(Key).getAction().getDescription());
						IActionChooser iActionChooser = new ActionChooser();
						com.esiag.isidis.fi.pdsTeam.receive.beans.Action action= new com.esiag.isidis.fi.pdsTeam.receive.beans.Action();
						action.setId(informationService.getById(mapKey).getAction().getId());
						ArrayList<Action> actionsList = new ArrayList<Action>();
						actionsList.add(action);
						action.setActionsList(actionsList);

						ActionChooser chooser = new ActionChooser();
						ActionExpander acEx = new ActionExpander();
						chooser.setmExpenderExecuter(acEx);
						ActionDeliver acDe = new ActionDeliver();
						chooser.setmNetFieldExecuter(acDe);

						chooser.chooseAction(action);
						return 1;

					}
					else
					{
						logger.log(Level.INFO, "l information envoyé au centre de controle est :" + informationService.getById(Key).getDescription());
						System.out.println("l information envoyé au centre de controle est :"+ informationService.getById(Key).getDescription());
						producer=new ActiveMQProducer(com.esiag.isidis.fi.pdsTeam.send.beans.Information.class);
						com.esiag.isidis.fi.pdsTeam.send.beans.Information information2 = new com.esiag.isidis.fi.pdsTeam.send.beans.Information();
						information2.setId(informationService.getById(Key).getId());
						information2.setDescri(informationService.getById(Key).getDescription());
						try {
							producer.send(information2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return 2;

					}
				}
			}
		}
		


			} catch (SecurityException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return 0;
	}
	

}
